#include <STC15F2K60S2.H>

unsigned char Keynum;
unsigned char num = 0;

void Delay_ms(int xms)	//15单片机延时函数
{
	int i;
	long a;
	for (i = xms; i > 0; i --)
		for(a = 810; a > 0; a --);
}

unsigned char Duli_Anjian(void)
{
	unsigned char Keynumber = 0;
	if(P30 == 0) {Delay_ms(20); while(P30 == 0); Delay_ms(20); Keynumber = 7;}
	if(P31 == 0) {Delay_ms(20); while(P31 == 0); Delay_ms(20); Keynumber = 6;}
	if(P32 == 0) {Delay_ms(20); while(P32 == 0); Delay_ms(20); Keynumber = 5;}
	if(P33 == 0) {Delay_ms(20); while(P33 == 0); Delay_ms(20); Keynumber = 4;}
	return Keynumber;
}

void main()
{
	P2 = 0xA0; P0 = 0x00; P2 = 0x80; P0 = 0xFF;		//初始化
	while(1)
	{
		Keynum = Duli_Anjian();	//调用按键检测函数
		if(Keynum == 7)	//第一个按键
		{
			num++;	//实现二进制加法
			P0 = ~num;
		}
		else if(Keynum == 6)	//第二个按键
		{
			num--;	//减法
			P0 = ~num;
		}
		else if(Keynum == 5)
		{
			P0 = 0x55;	// 0101 0101
		}
		else if(Keynum == 4)
		{
			P0 = 0xAA;	// 1010 1010
		}
	}
}
